package com.bdrm.service.impl;

import com.bdrm.dto.GoodsSalesDTO;
import com.bdrm.entity.Orders;
import com.bdrm.mapper.OrderMapper;
import com.bdrm.mapper.UserMapper;
import com.bdrm.service.ReportService;
import com.bdrm.vo.OrderReportVO;
import com.bdrm.vo.SalesTop10ReportVO;
import com.bdrm.vo.TurnoverReportVO;
import com.bdrm.vo.UserStatisticsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        // 计算出日期
        ArrayList<LocalDate> dataList = new ArrayList<>();

        //开始时间
        dataList.add(begin);
        // 如果开始时间不等于结束时间，则循环加1天
        while (!begin.equals(end)){
             begin = begin.plusDays(1);
             dataList.add(begin);
        }

        // 算出每天营业额
        ArrayList<Double> turnoverList = new ArrayList<>();
        for ( LocalDate date : dataList){
            // 每天最小和最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map .put("begin", beginTime);
            map .put("end", endTime);
            map .put("status", Orders.COMPLETED);
            Double turnover =orderMapper.sumByMap(map);
            if (turnover == null){
                turnover = 0.0;
            }
            turnoverList.add(turnover);
        }
        return TurnoverReportVO
                .builder()
                .dataList(StringUtils.join( dataList, ","))
                .turnoverList( StringUtils.join(turnoverList, ","))
                .build();
    }

    @Override
    public UserStatisticsVO getUserStatistics(LocalDate begin, LocalDate end) {

        // 日期列表
        ArrayList<LocalDate> dataList = new ArrayList<>();

        dataList.add(begin);
        while (!begin.equals(end)){
             begin = begin.plusDays(1);
             dataList.add( begin);
        }

        // 新增用户列表
        List<Integer> newUserList =  new ArrayList<>();
        // 总用户数量
        List<Integer> totalUserList =  new ArrayList<>();

        for ( LocalDate date : dataList){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();

            // 总用户
            map.put("end", endTime);
            Integer totalUser = userMapper.countByMap(map);

            // 新增用户
            map.put("begin", beginTime);
            Integer newUser= userMapper.countByMap(map);
            totalUserList.add( totalUser);
            newUserList.add( newUser);
        }

        return UserStatisticsVO.builder()
                .dateList( StringUtils.join( dataList, ","))
                .totalUserList( StringUtils.join(totalUserList, ","))
                .newUserList( StringUtils.join(newUserList, ","))
                .build();

    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        // 日期列表
        ArrayList<LocalDate> dataList = new ArrayList<>();

        dataList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dataList.add( begin);
        }

        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        for ( LocalDate date : dataList){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            // 每天订单总数量
            Integer orderCount = getOrderCount(beginTime, endTime, null);
            // 每天有效订单
            Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }
        // 计算时间区域内的总订单
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
        // 计算时间区域内的总有效订单
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
        // 订单完成率
         Double orderCompletionRate = 0.0;
         if (totalOrderCount > 0){
             orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
         }
        return OrderReportVO
                .builder()
                .dateList( StringUtils.join( dataList, ","))
                .orderCountList(StringUtils.join( orderCountList, ","))
                .validOrderCountList( StringUtils.join( validOrderCountList, ","))
                .orderCompletionRate( orderCompletionRate)
                .totalOrderCount( totalOrderCount)
                .validOrderCount( validOrderCount)
                .build();
    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginTime, endTime, Orders.COMPLETED);

        // 同时stream流获取名称和数量
        List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        
        String nameList = StringUtils.join(names, ',');
        String numberList = StringUtils.join(numbers, ',');
        return SalesTop10ReportVO.builder()
                .nameList( nameList)
                .numberList( numberList)
                .build();
    }


    private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status) {
        Map map = new HashMap();
        // 获取订单数
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", Orders.COMPLETED);
        return orderMapper.countByMap(map);
     }
}
